﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Principal;
using System.Web.Security;

namespace Agents
{
    //this is the websites homepage. the login page.
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        
        /// <summary>
        /// Occurs after the user loggs in and redirects him the to the menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void LoggedIn(object sender, EventArgs e)
        {
            Queries queries = new Queries();

            // Checking if the user is an admin
            if (Roles.IsUserInRole(login.UserName, "Admin"))
            {
                FormsAuthentication.RedirectFromLoginPage(login.UserName, true);
                Response.Redirect("Admin/AdminMenu.aspx");
            }
            else if (Roles.IsUserInRole(login.UserName, "Agent"))
            {
                // Recieving the status of the agent
                string status = queries.GetAgentByUserName(login.UserName).Status;

                //if the agent is starting a shift or in between clients
                if (status == "במשמרת" || status == "לא במשמרת")
                {
                    Response.Redirect("Agent/AgentMenu.aspx");
                }
                //if the agent is on the way to a client or at a client
                else
                {
                    int clientId = queries.GetClient(status).Id;
                    Response.Redirect("Agent/StartShiftDisplay.aspx?clientId=" + clientId);
                }
            }
        }
    }
}